﻿using System;
using System.Collections.Generic;
using BLToolkit.Data;
using BLToolkit.Reflection;
using System.Configuration;
using System.Data;

/// <summary>
/// Класс для работы с сущностями
/// </summary>
public class DBEntity<T>
{
	public static void Init()
	{
		DbManager.AddConnectionString("conf", ConfigurationManager.ConnectionStrings["MainConnect"].ConnectionString);
		DbManager.DefaultConfiguration = "conf";
	}

    public static IList<T> Select_e(T o)
    {
        using (DbManager db = new DbManager())
        {
            IList<T> list = Select(o);
            list.Insert(0, TypeAccessor<T>.CreateInstanceEx());
            return list;
        }
    }

    public static IList<T> Select_e(T o, string TableName)
    {
        using (DbManager db = new DbManager())
        {
            IList<T> list = Select(o, TableName);
            list.Insert(0, TypeAccessor<T>.CreateInstanceEx());
            return list;
        }
    }

    public static IList<T> Select_e()
    {
        using (DbManager db = new DbManager())
        {
            IList<T> list = Select(TypeAccessor<T>.CreateInstanceEx());
            list.Insert(0, TypeAccessor<T>.CreateInstanceEx());
            return list;
        }
    }

    public static IList<T> Select_e(string TableName)
    {
        using (DbManager db = new DbManager())
        {
            IList<T> list = Select(TypeAccessor<T>.CreateInstanceEx(), TableName);
            list.Insert(0, TypeAccessor<T>.CreateInstanceEx());
            return list;
        }
    }

    public static IList<T> Select(T o)
    {
        return Select(o, typeof(T).Name);
    }

    public static IList<T> Select(T o, string TableName)
    {
        using (DbManager db = new DbManager())
        {
            return db.SetSpCommand(TableName + "_select", db.CreateParameters(o)).ExecuteList<T>();
        }
    }

    public static object SelectObject(T o, string TableName)
    {
        using (DbManager db = new DbManager())
        {
            return db.SetSpCommand(TableName + "_select", db.CreateParameters(o)).ExecuteScalar();
        }
    }

    public static IList<T> Select(Int64 Id, string TableName)
    {
        using (DbManager db = new DbManager())
        {
            return db.SetSpCommand(TableName + "_select", db.NullParameter("@SysAccount_id", Id)).ExecuteList<T>();
        }
    }

    public static IList<T> Select()
    {
        return Select(typeof(T).Name);
    }

    public static IList<T> Select(string TableName)
    {
        using (DbManager db = new DbManager())
        {
            return db.SetSpCommand(TableName + "_select").ExecuteList<T>();
        }
    }

    public static T Load(T o)
    {
        return Load(o, typeof(T).Name);
    }

    public static T Load(T o, string TableName)
    {
        using (DbManager db = new DbManager())
        {
            return db.SetSpCommand(TableName + "_select", db.CreateParameters(o)).ExecuteObject<T>();
        }
    }

    public static T Load(Int64 Id)
    {
        return Load(Id, typeof(T).Name);
    }

    public static T Load(Int64 Id, string TableName)
    {
        using (DbManager db = new DbManager())
        {
            return db.SetSpCommand(TableName + "_select", db.Parameter("@" + TableName + "_id", Id)).ExecuteObject<T>();
        }
    }

    public static Int64 Merge(T o)
    {
        return Merge(o, typeof(T).Name);
    }

    public static Int64 Merge(T o, string TableName)
    {
        using (DbManager db = new DbManager())
        {
            return DS.ToInt64(db.SetSpCommand(TableName + "_merge", db.CreateParameters(o)).ExecuteScalar());
        }
    }

    public static object Exec(T o, string spName)
    {
        using (DbManager db = new DbManager())
            return db.SetSpCommand(spName, db.CreateParameters(o)).ExecuteScalar();
	}

	public static object Exec(T o, string spName, bool IsEnumerableResult)
	{
		using (DbManager db = new DbManager())
			return IsEnumerableResult ?
				db.SetSpCommand(spName, db.CreateParameters(o)).ExecuteList<T>() :
				db.SetSpCommand(spName, db.CreateParameters(o)).ExecuteScalar();
	}

	public static object ExecAsDataset(T o, string spName)
	{
		using (DbManager db = new DbManager())
			return db.SetSpCommand(spName, db.CreateParameters(o)).ExecuteDataSet();
	}

    public static Int32 Delete(Int64 Id)
    {
        return Delete(Id, typeof(T).Name);
    }

    public static Int32 Delete(Int64 Id, string TableName)
    {
        using (DbManager db = new DbManager())
        {
            return db.SetSpCommand(TableName + "_delete", db.Parameter("@" + TableName + "_id", Id)).ExecuteNonQuery();
        }
    }

    public static void Insert(T o)
    {
        using (DbManager db = new DbManager())
        {
            string _type = typeof(T).Name;
            db.SetSpCommand(_type + "_insert", db.CreateParameters(o)).ExecuteNonQuery();
        }
    }
}

public class DBEntity<T, T2>
{
    public static IList<T> Select(T2 o)
    {
        return Select(o, typeof(T2).Name);
    }

    public static IList<T> Select(T2 o, string TableName)
    {
        using (DbManager db = new DbManager())
        {
            return db.SetSpCommand(TableName + "_select", db.CreateParameters(o)).ExecuteList<T>();
        }
    }
}
